# 匿名函数，lambda表达式
# max,min,sorted
from datetime import datetime


def lambdaFunc():
    salaries = {
        'xialuo': 3000000,
        'xishi': 10000,
        'dahai': 3000
    }

    print('max: ', max(salaries))
    print('max key: ', max(salaries, key=lambda key: salaries[key]))
    print('min key: ', min(salaries, key=lambda key: salaries[key]))
    print('sorted true: ', sorted(salaries, key=lambda name: salaries[name], reverse=True))
    print('sorted false: ', sorted(salaries, key=lambda name: salaries[name], reverse=False))

    nums = [11, 33, 22, 9, 1]
    print('sorted true: ', sorted(nums, reverse=True))
    print('sorted false: ', sorted(nums, reverse=False))


# lambdaFunc()


# 递归：传递、回溯
def recursiveFunction(n):
    def factorial(num, res):
        print(num, res)
        if num < 3:
            return res
        return factorial(num - 1, (num - 1) * res)

    return factorial(n, n)


# print(recursiveFunction(5))


# 闭包函数
def closureFunc(x, y):
    def func():
        print(x + y)

    return func


# print(type(closureFunc(3, 3)))
# closureFunc(1, 2)()

# 装饰器
def run_time(func):
    def new_func(*args, **kwargs):
        start_time = datetime.now()
        print('开始执行%s：%s' % (func.__name__, start_time))
        func(*args, **kwargs)
        end_time = datetime.now()
        print('%s执行完毕：%s' % (func.__name__, end_time))
        cost = end_time - start_time
        print('%s执行花费：%s' % (func.__name__, cost))

    return new_func


@run_time
def doSomeProc():
    for e in range(10000):
        pass


# doSomeProc()
